Crate langtag

source ·
Expand description

This crate provides an implementation of language tags defined by RFC5646 (BCP47).

Usage

You can easily parse new language from anything that provides a [u8] reference:

extern crate langtag;
use langtag::LanguageTag;

fn main() -> Result<(), langtag::Error> {
  let tag = LanguageTag::parse("fr-FR")?;
  assert_eq!(tag.language().unwrap().primary(), "fr");
  assert!(tag == "Fr-fr"); // comparison is case-insensitive.
  Ok(())
}

Note that LanguageTag::parse does not copy the data it is given, but only borrows it. You can create an owning LanguageTag instance by using LanguageTagBuf::parse_copy to copy the data, or simply LanguageTagBuf::new to move the data.

Once parsed, you can explore every component of the language tag using the provided functions.

Mutable language tags

When the language tags owns its buffer through Vec<u8>, it becomes possible to access the tag mutably to modify it.

extern crate langtag;
use std::convert::TryInto;
use langtag::LangTag;

fn main() -> Result<(), langtag::Error> {
  let mut tag = LangTag::parse_copy("fr-FR")?;
  tag.language_mut().set_primary("jp".try_into()?);
  tag.set_region(None);
  tag.extensions_mut().insert('f'.try_into()?, "bar".try_into()?);
  assert_eq!(tag, "jp-f-bar");
  Ok(())
}

Structs

Enums

Statics

Traits

Type Definitions